// components/count-down/index.js
var util = require('../../utils/util.js');
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    time: {
      type: Number,
      value: 0
    },
    minTime: {
      type: Number,
      value: 0
    },
    itemStyle: {
      type: String,
      value: "color:#fff;"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    timeData: {},
  },

  /**
   * 组件的方法列表
   */
  methods: {
    getCurrentTime(timer) {
      let time = this.data.time;
      let minTime = this.data.minTime;
      let currentTime = timer.days * 24 * 60 * 60 + timer.hours * 60 * 60 + timer.minutes * 60 + timer.seconds;

      if (minTime <= 0) {
        return currentTime;
      }
      if (currentTime <= minTime) {
        return minTime;
      }
      return currentTime;
    },
    formatSeconds(value) {
      var theTime = parseInt(value); // 需要转换的时间秒 
      var theTime1 = 0; // 分 
      var theTime2 = 0; // 小时 
      var theTime3 = 0; // 天
      if (theTime > 60) {
        theTime1 = parseInt(theTime / 60);
        theTime = parseInt(theTime % 60);
        if (theTime1 > 60) {
          theTime2 = parseInt(theTime1 / 60);
          theTime1 = parseInt(theTime1 % 60);
          if (theTime2 > 24) {
            //大于24小时
            theTime3 = parseInt(theTime2 / 24);
            theTime2 = parseInt(theTime2 % 24);
          }
        }
      }
      var result = '';
      if (theTime > 0) {
        result = "" + parseInt(theTime) + "秒";
      }
      if (theTime1 > 0) {
        result = "" + parseInt(theTime1) + "分" + result;
      }
      if (theTime2 > 0) {
        result = "" + parseInt(theTime2) + "小时" + result;
      }
      if (theTime3 > 0) {
        result = "" + parseInt(theTime3) + "天" + result;
      }
      return result;
    },
    formatSeconds2(value) {
      var theTime = parseInt(value); // 需要转换的时间秒 
      var theTime1 = 0; // 分 
      var theTime2 = 0; // 小时 
      var theTime3 = 0; // 天
      if (theTime > 60) {
        theTime1 = parseInt(theTime / 60);
        theTime = parseInt(theTime % 60);
        if (theTime1 > 60) {
          theTime2 = parseInt(theTime1 / 60);
          theTime1 = parseInt(theTime1 % 60);
          if (theTime2 > 24) {
            //大于24小时
            theTime3 = parseInt(theTime2 / 24);
            theTime2 = parseInt(theTime2 % 24);
          }
        }
      }
      var result = {
        days: parseInt(theTime3),
        hours: parseInt(theTime2),
        minutes: parseInt(theTime1),
        seconds: parseInt(theTime)
      };
      return result;
    },
    onChange(e) {
      let timer = e.detail;
      let currentTime = this.getCurrentTime(timer);
      let date = this.formatSeconds2(currentTime);
      timer = {
        ...date
      }
      // if (timer.days < 10) {
      //   timer.days = '0' + timer.days
      // }
      // if (timer.hours < 10) {
      //   timer.hours = '0' + timer.hours
      // }
      // if (timer.minutes < 10) {
      //   timer.minutes = '0' + timer.minutes
      // }
      // if (timer.seconds < 10) {
      //   timer.seconds = '0' + timer.seconds
      // }
      this.setData({
        timeData: timer,
      });
    },
  }
})